<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/10 10:01:22 $)
// $RCSfile: admin_maintenance.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/tickets/functions_maintenance.php");
require_once ("./modules/tickets/functions_search.php");
require_once ("./modules/tickets/functions_ticketsui.php");

require_once ("./includes/functions_html.php");
$template->loadLanguageSection("admin_tmaintenance");


/**
* ###############################################
* MAINTENANCE
* ###############################################
*/
if ($eventaction == "maintenance")
{
	if ($_GET["do"] == "rebuildpindex")
	{
		if (empty($_GET["postsperpass"]) || !is_numeric($_GET["postsperpass"]) || intval($_GET["postsperpass"]) <= 0)
		{
			$_postpass = "30";
		} else {
			$_postpass = intval($_GET["postsperpass"]);
		}

		if (trim($_GET["_startat"]) == "" || trim($_GET["_finishat"]) == "" || trim($_GET["_totalposts"]) == "")
		{
			emptyEntireSearchIndex();
			$_tcount = $dbCore->queryFetch("SELECT COUNT(*) AS totalposts, MIN(ticketpostid) AS minticketpostid, MAX(ticketpostid) AS maxticketpostid FROM `". TABLE_PREFIX ."ticketposts`;");
			$_totalposts = intval($_tcount["totalposts"]);
			$_startat = intval($_tcount["minticketpostid"]);
			$_finishat = intval($_tcount["maxticketpostid"]);
			$_starttime = DATENOW;
			$_wordcount = 0;
//			echo "Recounting Data<BR>";
		} else {
			$_totalposts = intval($_GET["_totalposts"]);
			$_startat = intval($_GET["_startat"]);
			$_finishat = intval($_GET["_finishat"]);
			$_starttime = intval($_GET["_starttime"]);
			$_wordcount = intval($_GET["_wordcount"]);
		}
		$_finishindex = $_startat+$_postpass;

		if (trim($_GET["_processcount"]) == "")
		{
			$_processcount = 0;
		} else {
			$_processcount = intval($_GET["_processcount"]);
		}

		// Now lets fetch the ticket posts
		unset($lastticketpostid);
		$_searchticketidlist = array();
		$dbCore->query("SELECT `ticketpostid`, `subject`, `contents` FROM `". TABLE_PREFIX ."ticketposts` WHERE `ticketpostid` >= '". intval($_startat) ."' ORDER BY `ticketpostid` ASC LIMIT ". intval($_postpass) .";", 2);
		while ($dbCore->nextRecord(2))
		{
			$lastticketpostid = $dbCore->Record2["ticketpostid"];
			$_searchticketidlist[] = $dbCore->Record2;
//			echo "Processing: $lastticketpostid<BR>";
			$_processcount++;
		}

		foreach ($_searchticketidlist as $key=>$val)
		{
			$_wordcount += buildTicketPostIndex($val["ticketpostid"], $val["subject"], $val["contents"]);
		}

		if ($_totalposts)
		{
			$percent = $_processcount*(100/$_totalposts);
		} else {
			$percent = 100;
		}
		if ($percent <= 100 && $_startat < $_finishat && $_processcount < $_totalposts)
		{
			$redirecturl = "index.php?_m=tickets&_a=maintenance&do=rebuildpindex&_startat=". (intval($lastticketpostid)+1) ."&_finishat=". intval($_finishat) ."&_totalposts=".intval($_totalposts)."&_processcount=".intval($_processcount)."&postsperpass=".intval($_postpass)."&_starttime=".intval($_starttime)."&_wordcount=".intval($_wordcount);
		}
//		echo "Post Pass: $_postpass<BR>Process Count: $_processcount<BR>Total Posts: $_totalposts<BR>Start At: $_startat<BR>Finish At: $_finishat<BR>Percent: $percent<BR>Last Ticket Post ID: $lastticketpostid";

		echo $template->displayTemplate("header.tpl");
		printMainTableHeader($_SWIFT["language"]["rebuildpindex"]);
		$pcolumn = array();
		$pcolumn[0]["align"] = "center";
		$pcolumn[0]["valign"] = "top";
		$pcolumn[0]["nowrap"] = true;
		$pcolumn[0]["value"] = '<span class="bigtext">'. number_format($percent,2).'%</span>';
		$pcolumn[0]["class"] = "rowhighlight";
		$pcolumn[0]["colspan"] = "4";
		printDataRow($pcolumn);

		$_postremaining = $_totalposts-$_processcount;
		$_avgposttime = (DATENOW-$_starttime)/$_processcount;
		$_timeremaining = $_postremaining*$_avgposttime;

		$infolist = $colarray = array();
		$infolist[] = array($_SWIFT["language"]["wordsprocessed"], number_format(intval($_wordcount), 0));
		$infolist[] = array($_SWIFT["language"]["postsprocesed"], number_format(intval($_processcount), 0));
		$infolist[] = array($_SWIFT["language"]["timeelapsed"], strColorDate(DATENOW-$_starttime));
		$infolist[] = array($_SWIFT["language"]["timeremaining"], strColorDate($_timeremaining, true));

		printDescRow($_SWIFT["language"]["generalinformation"], 4);
		$cnt = $index = 0;
		$lastindex = count($infolist)-1;
		for ($ii=0; $ii<count($infolist); $ii++)
		{
			$colarray[$index]["align"] = "left";
			$colarray[$index]["valign"] = "top";
			$colarray[$index]["nowrap"] = true;
			$colarray[$index]["value"] = $infolist[$ii][0];
			$colarray[$index]["class"] = "row1";
			$index++;
			$colarray[$index]["align"] = "left";
			$colarray[$index]["valign"] = "top";
			$colarray[$index]["nowrap"] = true;
			$colarray[$index]["value"] = $infolist[$ii][1];
			$colarray[$index]["class"] = "row2";
			$cnt++; $index++;

			if ($cnt == 2 || $ii == $lastindex)
			{
				printDataRow($colarray);
				$colarray = array();
				$index = 0;
				$cnt=0;
			}
		}
		printMainTableFooter();

		if (!empty($redirecturl))
		{
			echo '<script type="text/javascript">function nextIndexStep() {window.location.href="'. $redirecturl .'";} setTimeout("nextIndexStep();", 3000);</script>';
		} else {
			echo '<span class="mediumtext"><center><a href="javascript:window.close();">'. $_SWIFT["language"]["closewindow"] .'</a></center></span>';
		}
		echo $template->displayTemplate("footer.tpl");
		exit;
	}

/*	if ($_SWIFT["settings"]["tickets_searchtype"] == 2 && empty($infomessage))
	{
		$infomessage = $_SWIFT["language"]["indexseterror"];
	}
*/
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["maintenance"], 1);

	$interface->adminNavBar($_SWIFT["language"]["maintenance"], $_SWIFT["language"]["desc_maintenance"] , 5);
	printInfoBox($infomessage);

	printFormStart("rpostform", "index.php", "POST", false, "", "javascript:popupSmallWindow('". $_SWIFT["swiftpath"]."admin/index.php?_m=tickets&_a=maintenance&do=rebuildpindex&postsperpass='+document.rpostform.postsperpass.value);return false;");


	printMainTableHeader($_SWIFT["language"]["rebuildpindex"]);
	printNumberRow("postsperpass", $_SWIFT["language"]["postperpass"], $_SWIFT["language"]["desc_postperpass"], iif(empty($_POST["postsperpass"]), "35", $_POST["postsperpass"]));
	printSubmitRow($_SWIFT["language"]["rebuild"]);

	printMainTableFooter();

	printFormEnd();


	$interface->adminFooter();
}
?>